<?php

class ActionPurchase extends BaseAction
{
    public function run()
    {
        $oBid = new Bid();

        if ( Yii::app()->request->getIsPostRequest() && Yii::app()->user->isUser())
        {
            $oBid->setAttributesFromRequest('aBid');

            $aBid = Yii::app()->request->getParam('aBid');

            $oBid->user_id = Yii::app()->user->id;
            $oBid->date_created = date('Y-m-d H:i:s');
            $oBid->message = isset($aBid['message'])?$aBid['message']:'';

            if($oBid->save())
            {
                $oLot = Lot::model()->findByPk($oBid->lot_id);
                $oUser = User::model()->findByPk($oLot->user_id);

                $oEmailNotification = new EmailNotification();
                $oEmailNotification->sendEmailToUserCreateBid($oUser, $oBid, $oLot);

                Yii::app()->user->setFlash('lots_purchase', 'Ваше предложение добавлено.');
                Yii::app()->controller->redirect(Yii::app()->controller->createUrl('/purchase'));
            }
        }

        $this->template->oBid = $oBid;
        $this->template->aMyBids = $oBid->getMyBids(Yii::app()->user->id);
        $this->template->aLots = Lot::model()->getAvailableLots('sale');
    }
}